package com.yklx.youke.controller;

import com.yklx.youke.entity.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin(origins = "*")
public class OrderController {

    // 获取所有订单
    @GetMapping("/orders")
    public ResponseEntity<List<Order>> getAllOrders(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            // 模拟返回一些订单数据
            Order order1 = new Order();
            order1.setOrderId(UUID.randomUUID().toString());
            order1.setUserId("1");
            order1.setOrderType("课程购买");
            order1.setAmount(new BigDecimal("399.00"));
            order1.setStatus("已完成");
            order1.setCreatedAt(LocalDateTime.now());
            order1.setUpdatedAt(LocalDateTime.now());

            Order order2 = new Order();
            order2.setOrderId(UUID.randomUUID().toString());
            order2.setUserId("2");
            order2.setOrderType("优币充值");
            order2.setAmount(new BigDecimal("199.00"));
            order2.setStatus("已支付");
            order2.setCreatedAt(LocalDateTime.now().minusDays(1));
            order2.setUpdatedAt(LocalDateTime.now().minusDays(1));

            List<Order> orders = List.of(order1, order2);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    // 获取单个订单
    @GetMapping("/orders/{orderId}")
    public ResponseEntity<Order> getOrderById(@PathVariable String orderId) {
        try {
            // 模拟返回一个订单
            Order order = new Order();
            order.setOrderId(orderId);
            order.setUserId("1");
            order.setOrderType("课程购买");
            order.setAmount(new BigDecimal("399.00"));
            order.setStatus("已完成");
            order.setCreatedAt(LocalDateTime.now());
            order.setUpdatedAt(LocalDateTime.now());

            return ResponseEntity.ok(order);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    // 创建新订单
    @PostMapping("/orders")
    public ResponseEntity<Map<String, Object>> createOrder(@RequestBody Order order) {
        try {
            // 模拟创建订单
            order.setOrderId(UUID.randomUUID().toString());
            order.setCreatedAt(LocalDateTime.now());
            order.setUpdatedAt(LocalDateTime.now());
            order.setStatus("待支付");

            Map<String, Object> response = Map.of(
                "message", "订单创建成功",
                "orderId", order.getOrderId(),
                "success", true
            );
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "订单创建失败: " + e.getMessage(),
                "success", false
            ));
        }
    }

    // 更新订单
    @PutMapping("/orders/{orderId}")
    public ResponseEntity<Map<String, Object>> updateOrder(@PathVariable String orderId, @RequestBody Order order) {
        try {
            // 模拟更新订单
            order.setOrderId(orderId);
            order.setUpdatedAt(LocalDateTime.now());

            Map<String, Object> response = Map.of(
                "message", "订单更新成功",
                "success", true
            );
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "订单更新失败: " + e.getMessage(),
                "success", false
            ));
        }
    }

    // 删除订单
    @DeleteMapping("/orders/{orderId}")
    public ResponseEntity<Map<String, Object>> deleteOrder(@PathVariable String orderId) {
        try {
            // 模拟删除订单
            Map<String, Object> response = Map.of(
                "message", "订单删除成功",
                "success", true
            );
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "订单删除失败: " + e.getMessage(),
                "success", false
            ));
        }
    }

    // 更新订单状态
    @PutMapping("/orders/{orderId}/status")
    public ResponseEntity<Map<String, Object>> updateOrderStatus(
            @PathVariable String orderId,
            @RequestBody Map<String, String> request) {
        try {
            String newStatus = request.get("status");
            // 模拟更新订单状态
            Map<String, Object> response = Map.of(
                "message", "订单状态更新成功",
                "success", true,
                "newStatus", newStatus
            );
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "订单状态更新失败: " + e.getMessage(),
                "success", false
            ));
        }
    }
}